{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# ipycanvas: John Conway's Game Of Life"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Some of the following code is adapted from\n",
    "https://jakevdp.github.io/blog/2013/08/07/conways-game-of-life/"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%pip install -q ipycanvas"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import asyncio\n",
    "\n",
    "import numpy as np\n",
    "from ipycanvas import RoughCanvas, hold_canvas"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def life_step(x):\n",
    "    \"\"\"Game of life step\"\"\"\n",
    "    nbrs_count = sum(\n",
    "        np.roll(np.roll(x, i, 0), j, 1)\n",
    "        for i in (-1, 0, 1)\n",
    "        for j in (-1, 0, 1)\n",
    "        if (i != 0 or j != 0)\n",
    "    )\n",
    "    return (nbrs_count == 3) | (x & (nbrs_count == 2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def draw(x, canvas, color=\"black\"):\n",
    "    with hold_canvas(canvas):\n",
    "        canvas.clear()\n",
    "        canvas.fill_style = \"#FFF0C9\"\n",
    "        canvas.stroke_style = \"white\"\n",
    "        canvas.fill_rect(0, 0, canvas.width, canvas.height)\n",
    "\n",
    "        canvas.fill_style = color\n",
    "        canvas.stroke_style = color\n",
    "\n",
    "        living_cells = np.where(x)\n",
    "\n",
    "        rects_x = living_cells[0] * n_pixels\n",
    "        rects_y = living_cells[1] * n_pixels\n",
    "\n",
    "        canvas.fill_rects(rects_x, rects_y, n_pixels)\n",
    "        canvas.stroke_rects(rects_x, rects_y, n_pixels)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# fmt: off\n",
    "glider_gun =\\\n",
    "[[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0],\n",
    " [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0],\n",
    " [0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1],\n",
    " [0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1],\n",
    " [1,1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0],\n",
    " [1,1,0,0,0,0,0,0,0,0,1,0,0,0,1,0,1,1,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0],\n",
    " [0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0],\n",
    " [0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],\n",
    " [0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]]\n",
    "\n",
    "# fmt: on\n",
    "x = np.zeros((50, 70), dtype=bool)\n",
    "x[1:10, 1:37] = glider_gun"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "n_pixels = 15\n",
    "\n",
    "canvas = RoughCanvas(width=x.shape[0] * n_pixels, height=x.shape[1] * n_pixels)\n",
    "canvas.fill_style = \"#FFF0C9\"\n",
    "canvas.stroke_style = \"white\"\n",
    "canvas.fill_rect(0, 0, canvas.width, canvas.height)\n",
    "\n",
    "canvas"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "draw(x, canvas, \"#5770B3\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for _ in range(1_000):\n",
    "    x = life_step(x)\n",
    "    draw(x, canvas, \"#5770B3\")\n",
    "\n",
    "    await asyncio.sleep(0.1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
